using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace LeisureCenter.Core
{
    public enum IconSize
    {
        s32x32,
        s96x96
    }

    public class IconsManager
    {
        private ImageList[] imageLists = new ImageList[2];
        private static int[] IconSizeValues = new int[] { 32, 96 };

        public ImageList this[IconSize size] { get { return imageLists[(int)size]; } }

        public IconsManager()
        {
            for (IconSize size = IconSize.s32x32; size <= IconSize.s96x96; size++)
            {
                ImageList imageList = new ImageList();
                imageList.ImageSize = new Size(IconSizeValues[(int)size], IconSizeValues[(int)size]);
                imageList.ColorDepth = ColorDepth.Depth32Bit;
                imageLists[(int)size] = imageList;
            }
        }

        internal void Initialize()
        {
#if DEBUG
            DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\..\..\themes\default\icons");
#else
            DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath) + @"\..\themes\default\icons");
#endif
            foreach (DirectoryInfo directory in directoryInfo.GetDirectories(@"*x*"))
            {
                foreach (FileInfo fileInfo in directory.GetFiles())
                {
                    if (fileInfo.Extension.Equals(@".bmp", StringComparison.InvariantCultureIgnoreCase) || fileInfo.Extension.Equals(@".png", StringComparison.InvariantCultureIgnoreCase))
                    {
                        AddToImageLists(fileInfo, false);
                    }
                    else
                    if (fileInfo.Extension.Equals(@".ico", StringComparison.InvariantCultureIgnoreCase))
                    {
                        AddToImageLists(fileInfo, true);
                    }
                }
            }
        }

        private void AddToImageLists(FileInfo fileInfo, bool isIcon)
        {
            AddToImageLists(Path.GetFileNameWithoutExtension(fileInfo.Name), fileInfo, isIcon);
        }

        private void AddToImageLists(string key, FileInfo fileInfo, bool isIcon)
        {
            for (IconSize size = IconSize.s32x32; size <= IconSize.s96x96; size++)
            {
                if (imageLists[(int)size].Images.Keys.Contains(key))
                    return;

                if (isIcon)
                    imageLists[(int)size].Images.Add(key, new Icon(fileInfo.FullName, new Size(IconSizeValues[(int)size], IconSizeValues[(int)size])));
                else
                    imageLists[(int)size].Images.Add(key, new Bitmap(fileInfo.FullName));
            }
        }

        public void AddToImageLists(string fileName)
        {
            if (File.Exists(fileName))
            {
                FileInfo fileInfo = new FileInfo(fileName);
                if (fileInfo.Extension.Equals(@".bmp", StringComparison.InvariantCultureIgnoreCase) || fileInfo.Extension.Equals(@".png", StringComparison.InvariantCultureIgnoreCase))
                {
                    AddToImageLists(fileName, fileInfo, false);
                }
                else
                if (fileInfo.Extension.Equals(@".ico", StringComparison.InvariantCultureIgnoreCase))
                {
                    AddToImageLists(fileName, fileInfo, true);
                }
            }
        }
    }
}
